<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Membership
 * @version 	1.0
 * @author 		Rukan <rukanhitokiri@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
class Zmodule extends Module
{	
	/**
	 * Install & uninstall SQL
	 * 
	 * $param string $file
	 * @return boolean
	 */
  	protected function _sql($file)
  	{
  		if ((!file_exists(dirname(__FILE__).'/../../'.$file)) 
		   || (!$sql = file_get_contents(dirname(__FILE__).'/../../'.$file)))
				return false;
		$sql = str_replace(array('PREFIX_', 'ENGINE_TYPE'), array(_DB_PREFIX_, _MYSQL_ENGINE_), $sql);
		$sql = preg_split("/;\s*[\r\n]+/", $sql);
		
		foreach ($sql AS $query)
			if (!Db::getInstance()->Execute(trim($query)))
				return false;
		return true;
	}
	
	/**
	 * Add new tab in administration
	 * 	 
	 * $param array $data
	 * @return boolean
	 */
  	protected function _addAdminTab($data = array())
  	{
		$idTabParent = Tab::getIdFromClassName($data['parentClassName']);
    	$languages = Language::getlanguages(false);	/*All languages id*/
     	foreach ($languages as $langs) {
        	$tabNames[$langs['id_lang']] = $data['childTabName'];
    	}
    	$tab = new Tab();
		$tab->module = $this->name;
    	$tab->name = $tabNames;
    	$tab->class_name = $data['childClassName'];
    	$tab->id_parent = $idTabParent;
    	if (!$tab->save())
      		return false;
    	return true;
	}
	
	/**
	 * Remove tab in administration
	 * 
	 * @param string $className
	 * @return boolean
	 */
	protected function _removeAdminTab($className)
	{
		$idTab = Tab::getIdFromClassName($className);
		$tab = new Tab($idTab);
		if (!$tab->delete())
      		return false;
		return true;
	}
	
	/**
	 * display Form Configuration
	 * 
	 * @return string html
	 */
 	protected function _displayForm()
 	{
 		$this->_displayFormConfig();
 		$this->_displayExtraFormConfig();
 		$this->_displaySupport();
 		return $this->_html;
	}
	
	/**
	 * display Extra Form Config (All tab)
	 * 
     * @return string html
	 */
	protected function _displayExtraFormConfig()
	{
		
	}
	
	/**
	 * display Support
	 * 
     * @return string html
	 */
	protected function _displaySupport()
	{
		$this->_html .= '
 		<fieldset><legend><img src="'.$this->_path.'logo.gif" /> '.$this->l('Support').'</legend>
			<img src="'.$this->_path.'img/logo.jpg" style="float:right;margin-left:10px" />
			<br />
			'.$this->l('The full installation guide is available on').' <a href="http://zprofess.com" target="_blank" style="text-decoration:underline">'.$this->l('the official Zprofess website').'</a>.
			<br /><br />
			'.$this->l('Find more information at').' <a href="http://zprofess.com" target="_blank" style="text-decoration:underline">'.$this->l('http://zprofess.com').'</a>.
		</fieldset>
		<div class="clear"></div>';
	}
	
	/**
	 * display Form Config (All tab)
	 * 
     * @return string html
	 */
	protected function _displayFormConfig()
	{
		
	}

	/**
	 * add element for form
	 *
	 * @param string $type type element html render
	 * @param array	$fields
     * @return string html
	 */
	public function addElement($type, $fields = array())
	{	
		if ($type == 'text')
			return $this->renderTextInput($fields);
		elseif ($type == 'color')
			return $this->renderColorPicker($fields);
		elseif ($type == 'select')
			return $this->renderSelect($fields);
		elseif ($type == 'yesno')
			return $this->renderYesNoOption($fields);
		elseif ($type == 'button')
			return $this->renderButton($fields);
		elseif ($type == 'textarea')
			return $this->renderTextarea($fields);
	}
	
	/**
	 * display Color Picker
	 *
	 * @param array	$fields
     * @return string html
	 */
	public function renderColorPicker($fields = array())
	{
		// <script type="text/javascript" src="'._PS_JS_DIR_.'/jquery/jquery-colorpicker.js"></script>
		return '
		<label>'.$this->l($fields['label']).'</label>
		<div class="margin-form">
			<input type="color" width="50px" data-hex="true" class="color mColorPickerInput" name="'.$fields['name'].'" value="'.htmlentities(Configuration::get($fields['value']), ENT_COMPAT, 'UTF-8').'" />
		</div>
		<div class="clear"></div>';
	}
	
	/**
	 * display TextInput
	 *
	 * @param array	$fields
     * @return string html
	 */
	public function renderTextInput($fields = array())
	{
		return '	
		<label>'.$this->l($fields['label']).'</label>
		<div class="margin-form">
			<input name="'.$fields['name'].'" id="'.$fields['name'].'" type="text" class="text" value="'.Configuration::get($fields['value']).'"/>
			<p class="clear">'.$this->l($fields['note']).'</p>
		</div>
		<div class="clear"></div>';
	}
	
		
	/**
	 * display Select option
	 * 
	 * @param array	$fields
     * @return string html
	 */
	public function renderSelect($fields = array())
	{
		$html = '	
				<label>'.$this->l($fields['label']).'</label>
				<div class="margin-form">
					<select name="'.$fields['name'].'" id="'.$fields['name'].'">';
			foreach($fields['values'] as $value => $label)
			$html .= '	<option value="'.$value.'" '.(Configuration::get($fields['value']) == $value ? "selected='selected'" : '').'>'.$label.'</option>';
		
		$html .= '	</select>
					<p class="clear">'.$this->l($fields['note']).'</p>
				</div>
				<div class="clear"></div>';
		return $html;
	}
	
	/**
	 * display YesNo option radio option
	 * 
	 * @param array	$fields
     * @return string html
	 */
	public function renderYesNoOption($fields = array())
	{
		return '	
		<label>'.$this->l($fields['label']).'</label>
		<div class="margin-form">
			<input type="radio" name="'.$fields['name'].'" id="'.$fields['name'].'_on" value="1" '.($fields['value'] ? 'checked="checked" ' : '').'/>
			<label class="t" for="'.$fields['name'].'_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
			<input type="radio" name="'.$fields['name'].'" id="'.$fields['name'].'_off" value="0" '.(!$fields['value'] ? 'checked="checked" ' : '').'/>
			<label class="t" for="'.$fields['name'].'_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
			<p class="clear">'.$this->l($fields['note']).'</p>
		</div>
		<div class="clear"></div>';
	}
	
/**
	 * display button Save
	 * 
	 * @param array	$fields
     * @return string html
	 */
	public function renderButton ($fields = array()) {
		
		$html = '
			<center>
				<input style="width:100px !important;" type="submit" name="'.$fields['name'].'" value="'.$fields['value'].'" class="button" />
			</center>
		';
		return $html;
	}
	
	/**
	 * display Textarea
	 * @param array	$fields
     * @return string html
	 */
	public function renderTextarea ($fields = array()) {
		$html ='
		<label>'.$this->l($fields['label']).'</label>
		<div class="margin-form">
			<textarea class="rte" cols="100" rows="10" name="'.$this->l($fields['name']).'">'.Configuration::get($fields['value']).'</textarea>
			<p class="clear">'.$this->l($fields['note']).'</p>
		</div>
		<div class="clear"></div>';
		return $html;
	}
}